/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufes.ceunes.poo.view.consultas;

import br.ufes.ceunes.poo.model.dao.DAOGenerico;
import br.ufes.ceunes.poo.model.dao.FornecedorDAO;
import br.ufes.ceunes.poo.model.pojo.Fornecedor;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author alpha
 */
public class Fornecedores extends javax.swing.JFrame {

    /**
     * Creates new form Fornecedores
     */
    DAOGenerico dao;

    public Fornecedores() {
        initComponents();
        dao = new FornecedorDAO();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txInfo = new javax.swing.JTextField();
        tInformacao = new javax.swing.JLabel();
        bBuscar = new javax.swing.JButton();
        bAlterarProduto = new javax.swing.JButton();
        tTitulo = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tFornecedor = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        txInfo.setText("Nome");
        txInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txInfoActionPerformed(evt);
            }
        });

        tInformacao.setText("Nome:");

        bBuscar.setText("Buscar");
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });

        bAlterarProduto.setText("Alterar ");
        bAlterarProduto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAlterarProdutoActionPerformed(evt);
            }
        });

        tTitulo.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        tTitulo.setText("Busca de Fornecedor");

        tFornecedor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "ID", "Nome", "CNPJ", "Endereço"
            }
        ));
        tFornecedor.setShowHorizontalLines(false);
        tFornecedor.setShowVerticalLines(false);
        jScrollPane1.setViewportView(tFornecedor);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(188, 188, 188)
                .addComponent(bAlterarProduto)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(161, 161, 161)
                .addComponent(tTitulo)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(tInformacao)
                        .addGap(35, 35, 35)
                        .addComponent(txInfo)
                        .addGap(18, 18, 18)
                        .addComponent(bBuscar))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 22, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tTitulo)
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tInformacao)
                    .addComponent(bBuscar))
                .addGap(30, 30, 30)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addComponent(bAlterarProduto)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txInfoActionPerformed
        txInfo.setText("");
    }//GEN-LAST:event_txInfoActionPerformed

    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        String info = txInfo.getText();

        preencherTabela(((FornecedorDAO) dao).buscaPorNome(info));


    }//GEN-LAST:event_bBuscarActionPerformed

    private void bAlterarProdutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAlterarProdutoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bAlterarProdutoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Fornecedores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Fornecedores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Fornecedores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Fornecedores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Fornecedores().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAlterarProduto;
    private javax.swing.JButton bBuscar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tFornecedor;
    private javax.swing.JLabel tInformacao;
    private javax.swing.JLabel tTitulo;
    private javax.swing.JTextField txInfo;
    // End of variables declaration//GEN-END:variables
 private void preencherTabela(List<Fornecedor> fornecedores) {
        DefaultTableModel model = (DefaultTableModel) tFornecedor.getModel();
        model.setRowCount(0);
        for (Fornecedor c : fornecedores) {
            model.addRow(new Object[]{null, null, null, null});
            model.setValueAt(c.getId(), model.getRowCount() - 1, 0);
            model.setValueAt(c.getNome(), model.getRowCount() - 1, 1);
            model.setValueAt(c.getCnpj(), model.getRowCount() - 1, 2);
            model.setValueAt(c.getEndereco(), model.getRowCount() - 1, 3);
        }
    }
}
